using System;
using System.Text;
using System.Data;
using System.Collections;
using System.Collections.Generic;

using Framework;

namespace SchemaMonitor
{
    //Table-Row Class (Customisable half)
    public partial class CNodesArchived
    {
        #region Constants
        private static string JOIN_USER = string.Concat(TABLE_NAME, " INNER JOIN ", CUser.TABLE_NAME, " ON NodeUserId=UserId"); //Nullable => Use LEFT OUTER JOIN
        private static string JOIN_TYPE = string.Concat(TABLE_NAME, " INNER JOIN ", CType.TABLE_NAME, " ON NodeTypeId=TypeId"); //Nullable => Use LEFT OUTER JOIN
        #endregion

        #region Constructors (Public)
        //Default Connection String
        public CNodesArchived() : base() {}
        public CNodesArchived(Guid nodeId) : base(nodeId) {}
        
        //Alternative Connection String
        public CNodesArchived(CDataSrc dataSrc) : base(dataSrc) {}
        public CNodesArchived(CDataSrc dataSrc, Guid nodeId) : base(dataSrc, nodeId) {}
        
        //Transactional (shares an open connection)
        protected internal CNodesArchived(CDataSrc dataSrc, Guid nodeId, IDbTransaction txOrNull) : base(dataSrc, nodeId, txOrNull) {}        
        #endregion

        #region Default Values
        protected override void InitValues_Custom()
        {
            //Custom default values (e.g. DateCreated column)
            //Member variables (e.g. for child collections)
        }
        #endregion
        
        #region Default Connection String
        protected override CDataSrc DefaultDataSrc()  { return CDataSrc.Default;  }
        #endregion

        #region Members
        //Foreign Keys
        [NonSerialized()]   private CUser _user;
        //Child Collections

        //Xml Data (as high-level objects)

        #endregion

        #region Properties - Relationships
        //Relationships - Foriegn Keys (e.g parent)

        public CType Type { get { return CType.Cache.GetById(this.NodeTypeId); } }
        public CUser User
        {
            get
            {
                if (_user == null)
                {
                    lock (this)
                    {
                        if (_user == null)
                            _user = new CUser(this.NodeUserId);
                    }
                }
                return _user;
            }
            set
            {
                _user = value;
                _nodeUserId = null != value ? value.UserId : int.MinValue;
            }
        }

        //Relationships - Collections (e.g. children)

        #endregion

        #region Properties - Customisation
        //Derived/ReadOnly (e.g. xml classes, presentation logic)
        #endregion

        #region Save/Delete Overrides
        //Can Override base.Save/Delete (e.g. Cascade deletes, or insert related records)
        #endregion

        #region Custom Database Queries
        //For Stored Procs can use: MakeList (matching schema), or DataSrc.ExecuteDataset (reports etc)
        //For Dynamic sql, can use: SelectSum, SelectDistinct, SelectCount, SelectWhere (inherited methods)
        //                see also: SelectBy[FK], Search and Count (auto-generated sample queries)

        public CNodesArchived GetById(Guid nodeId)
        {
            CNodesArchivedList match = SelectById(nodeId);
            if (match.Count == 0)
                return null;
            return match[0];
        }
        #endregion

        #region Cloning
        public CNodesArchived Clone(CDataSrc target, IDbTransaction txOrNull) //, int parentId)
        {
            //Shallow copy: Copies the immediate record, excluding autogenerated Pks
            CNodesArchived copy = new CNodesArchived(this, target);

            //Deep Copy - Child Entities: Cloned children must reference their cloned parent
            //copy.SampleParentId = parentId;

            copy.Save(txOrNull);

            //Deep Copy - Parent Entities: Cloned parents also clone their child collections
            //this.Children.Clone(target, txOrNull, copy.NodeId);

            return copy;
        }
        #endregion

        #region ToXml
        protected override void ToXml_Custom(System.Xml.XmlWriter w)
        {
            //Store(w, "Example", this.Example)
        }
        #endregion
    }
}